<?php
namespace jy_area_dian;
defined('BY_JYA') or exit('error');
/*
	代收点	
*/
class daishou extends \plugin{
	/*
		指派订单
	*/
	public function create_order($order,$dian_id=0,$status=0,$type=0){
		if($this->setting['daitype']!=2 && !$dian_id){
			$dians=model('jy_area_dian/model')->get_dians($order);
			$dian_id=$dians[0]['id'];
		}
		if(!$status){
			$status=$dian_id?1:0;	
		}
		$shoporder=pdo_get('jy_weishop_order',array('id'=>$order['orderid'],'uniacid'=>$_SESSION['uniacid']),array('plugin_name','ordersn','realname','mobile','addressid','address'));
		$total=pdo_getsum('jy_weishop_order_goods',array('orderid'=>$order['orderid'],'uniacid'=>$_SESSION['uniacid']),'total');
		$address=$shoporder['address']?json_decode($shoporder['address'],true):$shoporder['addressid'];
		$address['name']=$address['name']?:$shoporder['realname'];
		$address['mobile']=$address['mobile']?:$shoporder['mobile'];
		$data=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$order['mid'],
				'orderid'=>$order['id'],
				'order_price'=>$order['price'],
				'sendtype'=>$order['sendtype']!=1?2:1,
				'daitype'=>$this->setting['daitype'],
				'realname'=>$address['name'],
				'mobile'=>$address['mobile'],
				'total'=>$total,
				'dian_id'=>$dian_id,
				'createtime'=>TIMESTAMP,
				'status'=>$status,
				'type'=>$type,
		);
		pdo_insert('jy_area_dian_order',$data);
	}
	/*
		指派收货点
	*/
	public function to_daishou($orderid=0,$dian_id=0){
		if(!$orderid || $dian_id){
			exi('请选择订单和收货点','error');
		}
		pdo_update('jy_area_dian_order',array('dian_id'=>$dian_id,'status'=>1,'uptime'=>TIMESTAMP),array('orderid'=>$orderid));
		$order=pdo_get('jy_weishop_order',array('id'=>$orderid,'uniacid'=>$_SESSION['uniacid']),array('api_data'));
		$order['api_data']=json_decode($order['api_data'],true);
		$order['api_data']['area_dianid']=intval($dian_id);
		pdo_update('jy_weishop_order',array('api_data'=>ijson_encode($order['api_data'])),array('id'=>$orderid,'uniacid'=>$_SESSION['uniacid']));
		return true;
	}
}